/*
 * @Author: cqg
 * @Date: 2020-11-10 23:18:05
 * @LastEditors: cqg
 * @LastEditTime: 2020-12-04 18:44:06
 * @Description: file content
 */

import {
    searchFixedWarehousingList,
    exportRegistration
} from "@/views/wy-asset-management/fixed-warehousing/api/fixed-warehousing.js";
import { getDict } from "~/utils";
import { mapGetters } from "vuex";

export default {
    name: "resource-manage",
    provide() {
        return {
            initList: this.query
        };
    },
    computed: {
        ...mapGetters(["permissions"])
    },
    data() {
        return {
            // 表格列表数据
            tableData: [],
            // 请求参数
            params: {
                current: 1,
                size: 20,
                keyword: ""
            },
            // 选中的数据
            selectDataArr: [],
            // 分页
            pager: {
                total: 0, // 总页数
                current: 1, // 当前页数
                size: 20 // 每页显示多少条
            },
            //入库按钮权限
            warehousingBut: false,
            //
            menuList: [{ bulkStorage: "批量入库" }, { export: "导出" }],
            loading: false,
            filterList: [
                {
                    label: "资产分类",
                    inputType: "dict",
                    dictName: "asset-classification",
                    name: "assetClassificationList"
                },
                {
                    label: "使用状况",
                    inputType: "dict",
                    dictName: "asset-use-status",
                    name: "useStatusList"
                }
            ]
        };
    },
    //载入事件
    beforeMount() {
        this.warehousingBut = this.permissions["warehousing"];
        // //查询
        this.query();
    },
    methods: {
        // 菜单点击监听
        handleMenuClick(type) {
            switch (type) {
                case "bulkStorage":
                    this.$router.push(
                        "/asset-management/fixed-warehousing-import"
                    );
                    break;
                case "export":
                    this.exportReg();
                    break;
            }
        },
        /**
         * 查询方法
         * @param param
         */
        query(params = {}) {
            let req = { ...this.params, ...params };
            !req.assetClassificationList && delete req.assetClassificationList;
            !req.useStatusList && delete req.useStatusList;
            searchFixedWarehousingList(req)
                .then(respData => {
                    const {
                        current,
                        size,
                        total,
                        records
                    } = respData.data.data;
                    this.tableData = records;
                    this.params = req;
                    this.pager = { total, current, size };
                    this.loading = false;
                })
                .catch(error => {
                    this.$message({
                        type: "error",
                        message: "查询失败"
                    });
                })
                .finally(() => (this.loading = false));
        },

        /**
         * 列表导出
         */
        exportReg(params = { size: 9999 }) {
            let req = { ...this.params, ...params };
            exportRegistration(req).then(res => {
                // 关闭loading
                this.loading = false;
                const content = res.data;
                const blob = new Blob([content]); // 构造一个blob对象来处理数据
                const fileName = "资产待入库记录.xls"; // 导出文件名
                // 对于<a>标签，只有 Firefox 和 Chrome（内核） 支持 download 属性
                // IE10以上支持blob但是依然不支持download
                if ("download" in document.createElement("a")) {
                    // 支持a标签download的浏览器
                    const link = document.createElement("a"); // 创建a标签
                    link.download = fileName; // a标签添加属性
                    link.style.display = "none";
                    link.href = URL.createObjectURL(blob);
                    document.body.appendChild(link);
                    link.click(); // 执行下载
                    URL.revokeObjectURL(link.href); // 释放url
                    document.body.removeChild(link); // 释放标签
                } else {
                    // 其他浏览器
                    navigator.msSaveBlob(blob, fileName);
                }
                this.btnSendTem = false;
            });
        },
        //    码值转换
        dictVal_assetClassification(row, column, cellValue) {
            let obj = getDict("asset-classification").find(
                p => p.value == cellValue
            );
            if (obj) {
                return obj.label;
            }
            return "";
        },
        //  * 码值转换
        dictVal_useStatusList(row, column, cellValue) {
            let obj = getDict("asset-use-status").find(
                p => p.value == cellValue
            );
            if (obj) {
                return obj.label;
            }
            return "";
        }
    }
};
